package com.hunliji.monitor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hunliji.common.exception.WarnException;
import com.hunliji.monitor.dao.mapper.mt.InformActionStrategyMapper;
import com.hunliji.monitor.dao.model.dataobject.InformActionStrategyDO;
import com.hunliji.monitor.service.api.InformActionStrategyService;
import com.hunliji.monitor.service.api.model.bo.InformActionStrategyBO;
import com.hunliji.monitor.service.api.model.param.QueryInformActionStrategyParam;
import com.hunliji.monitor.share.component.ComponentLocator;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 通知的行动策略
 * @author wuchen
 */
@Service
public class InformActionStrategyServiceImpl implements InformActionStrategyService {
    private static final Logger logger = LoggerFactory.getLogger(InformActionStrategyServiceImpl.class);
    @Resource
    private InformActionStrategyMapper informActionStrategyMapper;
    @Override
    public void save(InformActionStrategyBO informActionStrategyBO) {
        InformActionStrategyDO informActionStrategyDO = ComponentLocator.getConverter().convert(informActionStrategyBO,InformActionStrategyDO.class);
        informActionStrategyMapper.insert(informActionStrategyDO);
    }

    @Override
    public void delete(InformActionStrategyBO informActionStrategyBO) {
        if(Objects.isNull(informActionStrategyBO.getId())){
            throw new WarnException("id不存在，无法删除");
        }
        InformActionStrategyDO informActionStrategyDO = ComponentLocator.getConverter().convert(informActionStrategyBO,InformActionStrategyDO.class);
        informActionStrategyDO.setDeleted(true);
        informActionStrategyMapper.updateById(informActionStrategyDO);
    }

    @Override
    public void updateById(InformActionStrategyBO informActionStrategyBO) {
        InformActionStrategyDO informActionStrategyDO = ComponentLocator.getConverter().convert(informActionStrategyBO,InformActionStrategyDO.class);
        informActionStrategyMapper.updateById(informActionStrategyDO);
    }


    @Override
    public List<InformActionStrategyBO> query(QueryInformActionStrategyParam queryInformActionStrategyParam) {
        LambdaQueryWrapper<InformActionStrategyDO> queryWrapper = new LambdaQueryWrapper<InformActionStrategyDO>();
        if(Objects.nonNull(queryInformActionStrategyParam.getAlertChatBindId())){
            queryWrapper.eq(InformActionStrategyDO::getAlertChatBindId,queryInformActionStrategyParam.getAlertChatBindId());
        }
        if(StringUtils.isNotBlank(queryInformActionStrategyParam.getActionStrategyKey())){
            queryWrapper.eq(InformActionStrategyDO::getActionStrategyKey,queryInformActionStrategyParam.getActionStrategyKey());
        }
        if(Objects.nonNull(queryInformActionStrategyParam.getSeverity())){
            queryWrapper.eq(InformActionStrategyDO::getSeverity,queryInformActionStrategyParam.getSeverity());
        }
        queryWrapper.eq(InformActionStrategyDO::getDeleted,Boolean.FALSE);
        List<InformActionStrategyDO> informActionStrategyDOList = informActionStrategyMapper.selectList(queryWrapper);
        List<InformActionStrategyBO> informActionStrategyBOList = ComponentLocator.getConverter().convertList(informActionStrategyDOList,InformActionStrategyBO.class);
        return informActionStrategyBOList;
    }

    @Override
    public InformActionStrategyBO queryById(Long id) {
        InformActionStrategyDO informActionStrategyDO = informActionStrategyMapper.selectById(id);
        if(Objects.isNull(informActionStrategyDO)){
            return null;
        }
        return ComponentLocator.getConverter().convert(informActionStrategyDO,InformActionStrategyBO.class);
    }


}
